package com.example.tests;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.example.pages.HomePage;
import com.example.pages.InternalPage;
import com.example.pages.LoginPage;
import com.example.pages.MovieAddPage;
import com.example.pages.MoviePage;

import ru.esteru.selenium.factory.WebDriverFactory;

public class CreateNewMovieTest {
  private WebDriver driver;
  private WebDriverWait wait;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();
  private LoginPage loginPage;
  private MovieAddPage movieAddPage;
  private InternalPage somePage;
  private HomePage homePage;
  private MoviePage moviePage;

  @Before
  public void setUp() throws Exception {
    driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
    baseUrl = "http://localhost/";
    wait = new WebDriverWait(driver, 10);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void createNewMovieTest() throws Exception {
    openHomePage();
    loginAs("admin", "admin");
    goToAddingMoviePage();
    createNewMovie("NewMovie", "8", "MovieName", "Description", "1990", "120", "50", "Very good movie");
    assertEquals("Personal notes\nVery good movie", getNotes());
    logOut();
  }
  
  public String getNotes() {
	  return moviePage.notesField.getText();
	}

private void logOut() {
	somePage.menuLogoutLink.click();
    wait.until(alertIsPresent()).accept();
}

private void createNewMovie(String howToSearchInIMDB, String imdbRating, String movieName, String description, String movieYear, String timeLength, String rating, String notes) {
	movieAddPage.imdbsearchField.clear();
	movieAddPage.imdbsearchField.sendKeys(howToSearchInIMDB);
	movieAddPage.imdbidField.clear();
	movieAddPage.imdbidField.sendKeys(imdbRating);
	movieAddPage.nameField.clear();
	movieAddPage.nameField.sendKeys(movieName);
	movieAddPage.akaField.clear();
	movieAddPage.akaField.sendKeys(description);
	movieAddPage.yearField.clear();
	movieAddPage.yearField.sendKeys(movieYear);
	movieAddPage.durationField.clear();
	movieAddPage.durationField.sendKeys(timeLength);
	movieAddPage.ratingField.clear();
	movieAddPage.ratingField.sendKeys(rating);
	movieAddPage.notesField.clear();
	movieAddPage.notesField.sendKeys(notes);
	movieAddPage.submitButtonField.click();
}

private void goToAddingMoviePage() {
	homePage.addMovieLink.click();
}

private void loginAs(String username, String password) {
	loginPage.userNameField.clear();
    loginPage.userNameField.sendKeys(username);
    loginPage.passwordField.clear();
    loginPage.passwordField.sendKeys(password);
    loginPage.loginButton.click();
}

private void openHomePage() {
	driver.get(baseUrl + "php4dvd/");
	loginPage = PageFactory.initElements(driver, LoginPage.class);
	movieAddPage = PageFactory.initElements(driver, MovieAddPage.class);
	somePage = PageFactory.initElements(driver, InternalPage.class);
	homePage = PageFactory.initElements(driver, HomePage.class);
	moviePage = PageFactory.initElements(driver, MoviePage.class);
}

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}
